<template>
  <div class="xtx-city">
    <div class="select" @click="toggleDialog" :class="{ active }">
      <span class="placeholder" v-if="!selected.fullSels">请选择配送地址</span>
      <!-- 展示选择完的结果 -->
      <span class="value" v-else>{{ selected.fullSels }}</span>
      <span class="value"></span>
      <i class="iconfont icon-angle-down"></i>
    </div>

    <!-- 选择内容弹层 -->
    <div class="option" v-if="active">
      <span @click="selectCity(city)" class="ellipsis" v-for="city in cityList" :key="city.code">{{city.name}}</span>
    </div>
  </div>
</template>

<script>
import { ref, onMounted, reactive } from 'vue'

// axios获取城市列表
import axios from 'axios'
// 获取城市列表数据
const useCityList = () => {
  const cityList = ref([])

  // 解决第一次选择之后再打开没有数据
  // 缓存数据
  const localCityList = ref([])

  onMounted(async () => {
    const { data } = await axios({
      url: 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
    })

    cityList.value = data
    // localCityList.value = data //直接用等于号改了localCityList的地址，返回的localCityList还是原来的地址为空
    localCityList.value.push(...data)
  })

  return { cityList, localCityList }
}

export default {
  name: 'XtxCity',

  // 声明子派发的事件
  emits: ['change'],

  setup (props, { emit }) {
    // 控制弹层的显示
    const active = ref(false)
    const open = () => {
      active.value = true

      // 还原数据
      cityList.value = localCityList.value
    }
    const close = () => {
      active.value = false
    }

    const toggleDialog = () => {
      if (active.value) {
        close()
      } else {
        open()
      }
    }

    // 定义交互城市数据
    const selected = reactive({
      provinceCode: '', // 省code
      provinceName: '', // 省名称
      cityCode: '', // 城市code
      cityName: '', // 城市名称
      countryCode: '', // 地区code
      countryName: '', // 地区名
      // 最终选择
      fullSels: ''
    })

    // 获取城市列表
    const { cityList, localCityList } = useCityList()

    // 城市列表三级联动
    // 并保存已经选择好的数据，传递给父组件
    // 每一次选择的城市都应该保存->所以传入的数据为city
    // 根据level判断省市
    const selectCity = (city) => {
      cityList.value = city.areaList

      //   判断省市
      if (city.level === 0) {
        selected.provinceCode = city.code
        selected.provinceName = city.name
      } else if (city.level === 1) {
        selected.cityCode = city.code
        selected.cityName = city.name
      } else {
        selected.countryCode = city.code
        selected.countryName = city.name

        selected.fullSels = `${selected.provinceName} ${selected.cityName} ${selected.countryName}`

        // 选完之后，关闭弹框，派发事件
        toggleDialog()
        emit('change', selected)
      }
    }

    return {
      active,
      toggleDialog,
      cityList,
      selectCity,
      selected
    }
  }
}

</script>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  margin-left: 10px;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
